# DESCRIPTION
# 
#   Python-implementation of a two-dimensional discrete convolution filter.
# 
# HISTORY
# 
#   20100921 KP - Initial version
# 
# AUTHOR
# 
#   Koen Poppe, Department of Computer Science,
#   Katholieke Universiteit Leuven, Celestijnenlaan 200A,
#   B-3001 Heverlee, Belgium
#   Email:  Koen.Poppe@cs.kuleuven.be
# 
def imageconvolution(A,F):
	
	import numpy
	
	# Size of the image
	[m,n] = A.shape
	
	# Offsets to center of the filter
	r = F.shape[0]/2
	s = F.shape[1]/2
	
	# Clear by assigning the image scaled with the center value of the filter
	C = F[0+r,0+s]*A
	
	# Non-center filter values
	for i in range(-r,r+1):
		# Bounds for indexing the rows: [lrow,urow) and [lrow+i,urow+i) in [0,m)
		lrow = max(0,0-i)
		urow = min(m,m-i) # ensure 
		for j in range(-s,s+1):
			if i!=0 or j!=0:
				# Bounds for indexing the cols: [lrow,urow) and [lcol+j,ucol+j) in [0,m)
				lcol = max(0,0-j)
				ucol = min(n,n-j)
				C[lrow:urow,lcol:ucol] += F[-i+r,-j+s]*A[(lrow+i):(urow+i),(lcol+j):(ucol+j)]
	
	return C
